package com.wtf.dsp.admin.config.swagger;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.*;

@Slf4j
@ConditionalOnWebApplication
@ConditionalOnProperty(name = "spring.swagger.enable", havingValue = "true",matchIfMissing = false)
@EnableConfigurationProperties(SwaggerProperties.class)
@EnableSwagger2WebMvc
@Import(BeanValidatorPluginsConfiguration.class)
public class WtfSwaggerAutoConfiguration {

    public WtfSwaggerAutoConfiguration(){
        log.info("Enable AutoConfiguration : {}",WtfSwaggerAutoConfiguration.class.getSimpleName());
    }

    @Bean
    public Docket createRestApi(SwaggerProperties swaggerProperties) {
        log.info("Swagger Properties : {}",swaggerProperties);
        Set<String> protocols = new HashSet<>();
        protocols.add("https");
        protocols.add("http");
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .enable(swaggerProperties.isEnable())
                .apiInfo(apiInfo(swaggerProperties))
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .protocols(protocols)
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }
    /**
     * 授权信息全局应用
     * @param
     * @return
     */
    private List<SecurityContext> securityContexts() {
        return Collections.singletonList(SecurityContext.builder().securityReferences(Collections.singletonList(new SecurityReference("Authorization",new AuthorizationScope[]{
                new AuthorizationScope("global","Authorization")
        }))).build());
    }

    /**
     * 设置授权信息
     * @return
     */
    private List<? extends SecurityScheme> securitySchemes() {
        List<ApiKey> apiKeys = new ArrayList<>();
        ApiKey apiKey = new ApiKey("Authorization","Authorization","Header");
        apiKeys.add(apiKey);
        return apiKeys;
    }

    /**
     * 设置API信息
     * @param swaggerProperties
     * @return
     */
    private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
        SwaggerProperties.ApiInfo apiInfo = swaggerProperties.getApiInfo();
        SwaggerProperties.Author author = swaggerProperties.getAuthor();
        return new ApiInfoBuilder()
                .title(apiInfo.getTitle())
                .description(apiInfo.getDescription())
                .version(apiInfo.getVersion())
                .termsOfServiceUrl(apiInfo.getTermsOfServiceUrl())
                .license(apiInfo.getLicense())
                .licenseUrl(apiInfo.getLicenseUrl())
                .contact(new Contact(author.getName(),author.getUrl(),author.getEmail()))
                .build();
    }

}

